package id.net.iconpln.bb.server;

import id.net.iconpln.bb.client.service.LoginService;
import id.net.iconpln.bb.server.model.persistence.UserBb;
import id.net.iconpln.bb.server.service.SecureService;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.gwtrpcspring.RemoteServiceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@Repository("loginService")
public class LoginServiceImpl extends RemoteServiceServlet implements LoginService{
/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private SecureService secureService;
	private static final Log log = LogFactory.getLog(LoginServiceImpl.class);
    
    @Autowired
    public void setSecureService(SecureService secureService) {
        this.secureService = secureService;
    }
	
	public String loginServer(String name, String pass) {
		// TODO Auto-generated method stub
        List<UserBb> u = new ArrayList<UserBb>();
   		String p = "";
        System.out.println("Login name : " + name + " Password : " + pass);
        
        try{
//        	ClassPathXmlApplicationContext cx = new ClassPathXmlApplicationContext("applicationContext.xml");
//        	SecureService ss = (SecureService) cx.getBean("secureService");
//        	

//            if(ss == null){
//            	System.out.println("null service");       
//            }else{
//            	System.out.println("not null service");
//            }
//            u = ss.getUser(name, pass);
//            u = ss.getUserByLogin(name, pass);
           u = secureService.getUserByLogin(name, pass);
			System.out.println("total : " + u.size());
			
			for (int i = 0; i < u.size(); i++) {
				System.out.println("nama unit " + u.get(i).getModule());
			}
				
           if (u.size() > 0 ){
//        	if (user.equals("kasir")){
//               HttpServletRequest re = this.getThreadLocalRequest();
//               re.setAttribute(arg0, arg1)
        	   
                RemoteServiceUtil.getThreadLocalRequest().getSession().setAttribute("user", u.get(0).getUser());
                RemoteServiceUtil.getThreadLocalRequest().getSession().setAttribute("id_unit", u.get(0).getId_unit());
                RemoteServiceUtil.getThreadLocalRequest().getSession().setAttribute("nama_unit", u.get(0).getNama_unit());
                RemoteServiceUtil.getThreadLocalRequest().getSession().setAttribute("jenis_unit", u.get(0).getJenis_unit());
                RemoteServiceUtil.getThreadLocalRequest().getSession().setAttribute("module", u.get(0).getModule());
                RemoteServiceUtil.getThreadLocalRequest().getSession().setAttribute("leveluser", u.get(0).getLeveluser());

                System.out.println("Login name : " + u.get(0).getUser() + " Module  " + u.get(0).getModule());
                System.out.println("name  Unit: " + u.get(0).getNama_unit() + " id UNit  " + u.get(0).getId_unit());
                System.out.println("Jenis Unit: " + u.get(0).getJenis_unit());
                System.out.println("Level user: " + u.get(0).getLeveluser());
                
                
                p = "SUKSES;"+u.get(0).getUser()+";"+u.get(0).getUser();
           }else{
        	   p = "NO";
           }
        }catch(Exception ex){
            ex.printStackTrace();
        	log.error("Message : "+ex.getMessage());
        }
        return p;
	}
	
}
